<?php

namespace app\common\model;

use think\Model;

class UserGrapes extends Model
{
	// 完整数据表名
	protected $table = 'user_grapes';

	
	// 获取用户最后生成的葡萄数据
	public function getLastUserGrapes($mobile)
	{
		return $this->where(['mobile'=>$mobile])->order('create_time desc')->find();
	}
	// 获取用户当前可获取葡萄数量
	public function canGetGrapes($mobile) 
	{
		return $this->where(['mobile'=>$mobile,'status'=>1])->count();
	}
	// 增加用户可收集葡萄数据
	public function addUserGrapes($user,$num,$should)
	{
		if ($should > 0) {
			$add_data = [];
			for ($i = 0; $i < $should; $i++) {
				$add_data[] = [
					'uid' => $user['id'],
					'mobile' => $user['mobile'],
					'grapes' => $num,
					'status' => 1,
					'create_time' => time(),
					'create_date' => date('Y/m/d'),
				];
			}
			$this->saveAll($add_data);
		}
	}
	// 新用户默认生成
	public function newGrapes($user)
	{
		$num = sysconf('new_grapes');
		$add_data = [];
		for ($i = 0; $i < $num; $i++)
		{
			$add_data[] = [
				'uid' => $user['id'],
				'mobile' => $user['mobile'],
				'grapes' => ceil($user['calculation']/24/4*100000)/100000,
				'status' => 1,
				'create_time' => time(),
				'create_date' => date('Y/m/d')
 			];
		}
		$this->saveAll($add_data);
	}
	// 超时过期
	public function overdue($mobile)
	{
		$data = $this
		        ->where([
		        	'mobile'=>$mobile,
		        	'create_time'=>['>',time()-(3*24*60*60)],
		        ])
		        ->update(['status'=>3]);
	}
}